package ge;

import java.util.HashMap;
import java.util.Iterator;

import org.apache.log4j.Logger;

public abstract class NonTerminal implements Symbol {
	protected static final Logger LOG = Logger.getLogger(NonTerminal.class);

	private static HashMap<Class<?>, ProductionRule> _production_rules = new HashMap<Class<?>, ProductionRule>();

	private int _index;

	public void setIndex(int index) {
		_index = index;
	}
	
	public int getIndex() {
		return _index;
	}
	
	/**
	 * Turn the nonterminal of the given type into an expression by applying the
	 * appriopriate production rule.
	 * 
	 * @param clazz
	 *            the class of the nonterminal to try and resolve
	 * @param indices
	 *            an iterator to get the index of which alternative in the
	 *            production rule to use
	 * @param depth
	 *            the current recursion depth
	 * @return
	 */
	public static Expression resolve(Class<?> clazz, Iterator<Integer> indices,
			int depth) {
		Expression exp = null;

		exp = _production_rules.get(clazz).evaluate(indices,  depth);

		return exp;
	}

	/**
	 * Set the production rule used to turn the nonterminal of the given type
	 * into an expression.
	 * 
	 * @param clazz
	 * @param production_rule
	 */
	public static void setProductionRule(Class<?> clazz,
			ProductionRule production_rule) {
		_production_rules.put(clazz, production_rule);
	}

	/**
	 * Returns the java code representing this non-terminal;
	 * 
	 * @return
	 */
	public String getCode() { 
		return "";
	}

	/**
	 * Returns the production rule of the associated nonterminal class.
	 * 
	 * @param clazz
	 * @return
	 */
	public static ProductionRule getAssociatedProductionRule(Class<?> clazz) {
		return _production_rules.get(clazz);
	}
}
